/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import vn.webapp.model.Classing;
import vn.webapp.model.Coursing;

@Repository("classingDAO")
@SuppressWarnings({"unchecked", "rawtypes"})
@Transactional
public class ClassingDAOImpl extends BaseDao implements ClassingDAO {

    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public List<Classing> list() {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Classing.class, "classing");
            List<Classing> classing = criteria.list();
            commit();
            return classing;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }

    @Override
    public Classing checkExists(int id, int courseteacherid, int classId){
//        try {
//            begin();
//            Criteria criteria = getSession().createCriteria(Classing.class, "classing");
//            criteria.createAlias("classing.courseteacher", "courseteacher");
//            criteria.add(Restrictions.eq("courseteacher.id",courseteacherid));
//            
//            criteria.createAlias("classing.courseteacher", "courseteacher");
//            criteria.add(Restrictions.eq("courseteacher.id",classId));
//            
//            criteria.add(Restrictions.ne("classing.id",id));
//            Classing coursing = (Classing)criteria.uniqueResult();
//            commit();
//            return coursing;
//        } catch (HibernateException e) {
//            e.printStackTrace();
//            rollback();
//            close();
//            return null;
//        } finally {
//            flush();
//            close();
//        }
        return new Classing();
    }
            
    @Override
    public int removeAClassing(int id){
        Classing c = new Classing();
        c.setCourseteacherclass_id(id);        
        try {
            begin();
            getSession().delete(c);
            commit();
            return 1;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public int saveAClassing(Classing classing)
    {
        int id = 0; 
        try {
           begin();           
           id = (int)getSession().save(classing);
           commit();
           return id;           
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public void editAClassing(Classing classing){
        try {
           begin();
           getSession().update(classing);
           commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public List<Classing> getByIds(List<Integer> ids){
        try {
        begin();
        Criteria criteria = getSession().createCriteria(Classing.class, "classing");
        List<Classing> classings = new ArrayList<>();
        
        criteria.add(Restrictions.in("classing.courseteacherid",ids));
        classings = (List<Classing>) criteria.list();
        commit();
        return classings;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public List<Classing> getById(int courseteacherid){
        try {
        begin();
        Criteria criteria = getSession().createCriteria(Classing.class, "classing");
        List<Classing> classings = new ArrayList<>();
        
        criteria.add(Restrictions.eq("classing.courseteacher_id", courseteacherid));
        classings = (List<Classing>) criteria.list();
        commit();
        return classings;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public Classing getAClassingByCourseAndClassId(int id, int courseId, int classId)
    {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Classing.class, "classing");
            criteria.add(Restrictions.eq("classing.classid", classId));
            criteria.add(Restrictions.eq("classing.courseteacherid", courseId));
            criteria.add(Restrictions.ne("classing.id", id));
            Classing oClassing = (Classing) criteria.uniqueResult();
            commit();
            return oClassing;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public int removeAClassingByCourseTeacherId(int courseteacherid){ 
        Classing classing = new Classing();
        classing.setCourseteacher_id(courseteacherid);
        try {
            begin();
            getSession().delete(classing);
            commit();
            return 1;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
}
